package com.sl.dynamic_proxy;

import java.lang.reflect.Proxy;

/**
 * author： suliang
 * 2023/1/4 17:41
 * describe : 动态代理测试类
 */
public class DynamicProxyTest {
    public static void main(String[] args) {
        //被代理类
        Subject realSubject = new RealSubject_1() ;
        //要代理哪个类，就传递哪个类进行InvocationHandler中
        SubjectInvocationHandler handler = new SubjectInvocationHandler(realSubject) ;
        //生成代理类
        Subject subject  = (Subject) Proxy.newProxyInstance(handler.getClass().getClassLoader(), new Class[]{Subject.class}, handler);
//        Proxy.newProxyInstance(handler.getClass().getClassLoader(),RealSubject_1.class.getInterfaces(),handler);
        //调用
        System.out.println(subject.sayHello());
        subject.sayGoodBye();
        System.out.println("********************");
        System.out.println("Proxy : " + subject.getClass().getName());
        System.out.println("Proxy : " + subject.getClass().getSuperclass().getName());
        System.out.println("Proxy : " + subject.getClass().getInterfaces()[0].getName());

    }
}
